package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("套餐管理")
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(value = "setmealCache",key = "#dto.categoryId")
    public Result add(@RequestBody SetmealDTO dto){
        log.info("新增套餐{}",dto);
        setmealService.add(dto);
        return Result.success();
    }
    /**
     * 分页条件查询套餐
     * @param dto
     * @return
     */
    @ApiOperation("分页条件查询套餐")
    @GetMapping("/page")

    public Result page(SetmealPageQueryDTO dto){
        log.info("分页查询套餐{}",dto);
         PageResult p  =setmealService.page(dto);
        return Result.success(p);
    }
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long>ids){
        log.info("批量删除套餐{}",ids);
       boolean b= setmealService.delete(ids);
       if (b){
        return Result.success();}
       return Result.error("还有套餐处于启售状态！无法删除");
    }
    @ApiOperation("查询回显")
    @GetMapping("/{setmealId}")
    public Result getById(@PathVariable Integer setmealId){
        log.info("查询回显{}",setmealId);
       SetmealVO s= setmealService.getById(setmealId);
       log.info(s.getSetmealDishes().toString());
       return Result.success(s);
    }
    @ApiOperation("修改套餐")
    @PutMapping()
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO dto){
        log.info("修改套餐{}",dto);
        setmealService.update(dto);

        return Result.success();
    }
    @ApiOperation("启售/停售")
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result saleStatus(@PathVariable Integer status,@RequestParam(required = false)Integer id){
        log.info("启售/停售{}/{}",status,id);
        if(setmealService.saleStatusChange(status,id)){
            return Result.success();
        }else {
            return Result.error("套餐内含有停售菜品！");
        }

    }
}
